//
//  CirclePostListViewController.m
//  安好
//
//  Created by rlp on 14-11-28.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "CirclePostListViewController.h"

@interface CirclePostListViewController (){
//    UILabel *description;
}
@property(nonatomic,strong) UILabel *description1;
@property(nonatomic,strong)UIView *headerView;
@property(nonatomic,strong)UIView *topView;

@property(nonatomic,assign)BOOL isExpanded;
@end

@implementation CirclePostListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    if (!self.groupUserModel) {
        self.groupUserModel = [[GroupUserModel alloc]init];
    }
    if (!self.threadBaseClass) {
        self.threadBaseClass = [[ThreadBaseClass alloc]init];
    }
    self.threadPage =1;

    
     self.edgesForExtendedLayout=UIRectEdgeNone;
    [self makeNavBarView];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, kScreenHeight-64-20) style:UITableViewStylePlain];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView addFooterWithTarget:self action:@selector(loadWithData)];
    self.tableView.frame = CGRectMake(0, 0, self.view.frame.size.width, kScreenHeight-64);
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];//去掉系统的分割线
    [self.view addSubview:self.tableView];
    [self initWithHeaderView];
    [self loadWithData];
}
-(void)loadWithData{
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithCapacity:10];
    
    [dict setObject:kPlatform forKey:@"platform"];
    [dict setObject:kVersion forKey:@"version"];
    [dict setObject:[NSString stringWithFormat:@"%ld",(long)self.threadPage] forKey:@"page"];
    [dict setObject:[self.groupUserModel gid] forKey:@"gid"];
    [dict setObject:@"0" forKey:@"digest"];//1为精华帖

    [dict setObject:@"dateline" forKey:@"order"];//为排序 lastpost最新回复
    

    
    
    
    
    //帖子列表
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager POST:[NSString stringWithFormat:@"%@%@",kBaseURL,THREAD] parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSNumber * statusNum = [responseObject objectForKey:@"status"];
        NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
        NSString * statusString = [numberFormatter stringFromNumber:statusNum];
        if ([statusString isEqualToString:@"0"]) {
            alertContent([responseObject objectForKey:@"msg"]);
        }else{
            
            
            
            if (self.threadPage>1) {
                [self.threadBaseClass.data addObjectsFromArray:[ThreadBaseClass modelObjectWithDictionary:responseObject].data];

            }else{
                self.threadBaseClass = [ThreadBaseClass modelObjectWithDictionary:responseObject];//基类 数据模型

            }
            
            
            NSArray *jsonDictArr = [self.threadBaseClass.data isEqual:[NSNull null]]?nil:self.threadBaseClass.data;
            
            if (jsonDictArr!=nil) {
                self.threadPage++;
                [self.tableView reloadData];
            }else{
                if (self.threadPage>1) {
                    MyNSLog("下拉无数据");
                }
            }

            
        }
        
        [self.tableView footerEndRefreshing];

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self.tableView footerEndRefreshing];

    }];

}
-(void)initWithHeaderView{
    
    UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
    [searchBar setContentMode:UIViewContentModeLeft];
    
    
    
    
    
    
    
    
    
    self.headerView = [[UIView alloc]initWithFrame:CGRectMake(0, searchBar.frame.size.height, kScreenWidth, 40)];
    self.headerView.backgroundColor = [ClassMethodsViewController colorWithHexString:@"#ecf4f6"];
    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handelTap:)];
    [self.headerView addGestureRecognizer:tapGes];
    
//    self.tableView.tableHeaderView = self.headerView;
    
    UIImageView *tagImage = [[UIImageView alloc]initWithFrame:CGRectMake(15, 10, 22, 20)];
    [tagImage setImageURLStr:self.groupUserModel.icon placeholder:nil];
    [self.headerView addSubview:tagImage];
    
    
    
    //hui
    UIButton *buttonThreads = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 0, 15)];
    [buttonThreads setImage:[UIImage imageNamed:@"圈子_人数"] forState:UIControlStateNormal];
    [buttonThreads setTitle:[NSString stringWithFormat:@" %ld ",(long)[self.groupUserModel.threads integerValue]] forState:UIControlStateNormal];
    [buttonThreads setTitleColor:TITLE_ARTICLE_CONTENTFONT_COLOR forState:UIControlStateNormal];
    buttonThreads.titleLabel.font = [UIFont systemFontOfSize:DVSWITCH_YEXT_FONT];
    buttonThreads.userInteractionEnabled = NO;
    [buttonThreads sizeToFit];
    
    UIButton *buttonPosts = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 0, 15)];
    [buttonPosts setImage:[UIImage imageNamed:@"圈子_评论.png"] forState:UIControlStateNormal];
    [buttonPosts setTitle:[NSString stringWithFormat:@" %ld ",(long)[self.groupUserModel.posts integerValue]] forState:UIControlStateNormal];
    [buttonPosts setTitleColor:TITLE_ARTICLE_CONTENTFONT_COLOR forState:UIControlStateNormal];
    buttonPosts.titleLabel.font = [UIFont systemFontOfSize:DVSWITCH_YEXT_FONT];
    buttonPosts.userInteractionEnabled = NO;
    [buttonPosts sizeToFit];
    buttonPosts.frame = CGRectMake(buttonThreads.frame.size.width, buttonPosts.frame.origin.y, buttonPosts.frame.size.width, buttonPosts.frame.size.height);
    
    UIView *viewbutton = [[UIView alloc]initWithFrame:CGRectMake(kScreenWidth-buttonPosts.frame.size.width-buttonThreads.frame.size.width-15, tagImage.frame.origin.y, buttonPosts.frame.size.width+buttonThreads.frame.size.width, buttonThreads.frame.size.height)];
    
    [viewbutton addSubview:buttonThreads];
    [viewbutton addSubview:buttonPosts];
    
    [self.headerView addSubview:viewbutton];

    
    
    
    
    
    
    
    UILabel *titleName =[[UILabel alloc]initWithFrame:CGRectMake(tagImage.frame.size.width+tagImage.frame.origin.y+5, tagImage.frame.origin.y, viewbutton.frame.origin.x-5-(tagImage.frame.size.width+tagImage.frame.origin.y+5), 20) text:[NSString stringWithFormat:@"%@",self.groupUserModel.title] font:[UIFont systemFontOfSize:TITLE_TEXTLABEL_FOUNT_SIZE] textColor:TITLE_ARTICLE_TEXTFONT_COLOR backgroundColor:[UIColor clearColor] numberOfLines:0 lineSpacing:2];
    
    
    
    
    
    [self.headerView addSubview:titleName];


    
    self.description1 =[[UILabel alloc]initWithFrame:CGRectMake(15, 40, kScreenWidth-15*2, 0) text:[NSString stringWithFormat:@"%@",self.groupUserModel.description1] font:[UIFont systemFontOfSize:LABEL_FONT_SIZE_CONTENT] textColor:TITLE_ARTICLE_CONTENTFONT_COLOR backgroundColor:[UIColor clearColor] numberOfLines:0 lineSpacing:5];
    [self.headerView addSubview:self.description1];
    self.description1.alpha = 0.f;
    
    
    
    
    self.topView = [[UIView alloc]initWithFrame:CGRectMake(0, 44, kScreenWidth, self.headerView.frame.size.height+self.headerView.frame.origin.y)];
    [self.topView addSubview:searchBar];
    [self.topView addSubview:self.headerView];
    
    
    self.tableView.tableHeaderView = self.topView;
    
    
    
    
    
}
-(IBAction)handelTap:(id)sender{
    
    
    [UIView animateWithDuration:0.6 animations:^
     {
         if (self.isExpanded) {//是展开的
             self.headerView.frame =CGRectMake(0, self.headerView.frame.origin.y, kScreenWidth, 40);
             self.topView.frame =CGRectMake(0, self.topView.frame.origin.y, kScreenWidth, self.headerView.frame.size.height+self.headerView.frame.origin.y);

             
             
             
             [UIView animateWithDuration:0.4 animations:^
              {
                  self.description1.alpha = 0.f;
                  
              } completion:^(BOOL finished)
              {
              }];
         }else{
             
             self.headerView.frame =CGRectMake(0, self.headerView.frame.origin.y, kScreenWidth, self.description1.frame.size.height+self.description1.frame.origin.y+10);
             self.topView.frame =CGRectMake(0, self.headerView.frame.origin.y, kScreenWidth,  self.headerView.frame.size.height+self.headerView.frame.origin.y);


             [UIView animateWithDuration:0.8 animations:^
              {
                  self.description1.alpha = 1.f;
                  
              } completion:^(BOOL finished)
              {
              }];
             
         }
         
         self.tableView.tableHeaderView = self.topView;
         
     } completion:^(BOOL finished)
     {
     }];
    self.isExpanded = !self.isExpanded;
}



#pragma mark - 设置导航条
-(void)makeNavBarView
{
    self.navigationItem.title = [NSString stringWithFormat:@"%@",self.groupUserModel.title];
    //贴图
    UIBarButtonItem *backBtnImg = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_返回.png"] style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonClicked:)];
    backBtnImg.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = backBtnImg;
    
}

-(void)leftButtonClicked:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    NSInteger number=0;
        number = 1;
      return number;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    NSInteger number=0;
        number = self.threadBaseClass.data.count;

    return number;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger hight=44;
        hight = [self tableView:tableView cellForRowAtIndexPath:indexPath].frame.size.height;
     return hight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
    
    static NSString *ID=@"tableViewCell";
    SquareATableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
        if (cell == nil) {
            //            cell = [[[NSBundle mainBundle] loadNibNamed:@"SquareTableViewCell" owner:self options:nil] lastObject];
            cell = [[SquareATableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        }
        
        [[cell title] setText:[NSString stringWithFormat:@"%@",[[self.threadBaseClass.data objectAtIndex:indexPath.row] subject]]];
        [[cell content] setText:[NSString stringWithFormat:@"%@",[[self.threadBaseClass.data objectAtIndex:indexPath.row] summary]]];
        
        
        
        NSMutableArray *arrimages = [[NSMutableArray alloc ]initWithArray:[[self.threadBaseClass.data objectAtIndex:indexPath.row] pic]];
        
        
        [cell setSquareCellImageViewsArray:arrimages];
        
        [[cell timesLabel] setText:[NSString stringWithFormat:@"%@",[(CircleModelData*)[self.threadBaseClass.data objectAtIndex:indexPath.row] dateline]]];
        
        [cell bottomView].tag =indexPath.row;
        
        
        
        NSArray *numberArry =[[NSArray alloc]initWithObjects:[NSString stringWithFormat:@"%ld",(long)[[NSString stringWithFormat:@"%@",[[self.threadBaseClass.data objectAtIndex:indexPath.row] tid]] integerValue]],
                              [NSString stringWithFormat:@"%ld",(long)[[NSString stringWithFormat:@"%@",[[self.threadBaseClass.data objectAtIndex:indexPath.row] good]] integerValue]],
                              [NSString stringWithFormat:@"%ld",(long)[[NSString stringWithFormat:@"%@",[[self.threadBaseClass.data objectAtIndex:indexPath.row] replies]] integerValue]],
                              nil];
        [(SquareATableViewCell*)cell setArrayNumber:numberArry];
        
        
        NSMutableArray *array =[[NSMutableArray alloc]initWithCapacity:10];
        if ([[NSString stringWithFormat:@"%@",[[self.threadBaseClass.data objectAtIndex:indexPath.row] digest]] isEqualToString:@"1"]) {
            [array addObject:[NSNumber numberWithInteger:SquareMarkStyleRefinement]];
        }
        
        
        
        
        [cell setSquareMarkStyles:array];
        
        [cell squareCellImageView].tag = indexPath.row;
        
//        [cell setDelegate:self];
    
        
        
        
        
        
      // Configure the cell...
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
CirclePostsDetailsViewController *cpdVC = [[CirclePostsDetailsViewController alloc]
                                           initWithNibName:@"CirclePostsDetailsViewController" bundle:nil];
//    cpdVC = (CircleModelData*)[self.threadBaseClass.data objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:cpdVC animated:YES];
}
@end
